package ai.people.core.web.config;

import ai.people.core.web.filter.fastjson.SymbolConversionFilter;
import ai.people.core.web.filter.fastjson.ValueDesensitizeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;

import java.util.ArrayList;
import java.util.List;

/**
 * fastjson配置
 *
 * @author yuanqinglong
 * @date 2022/3/8 14:40
 */
@Configuration
@Import({SymbolConversionFilter.class, ValueDesensitizeFilter.class})
public class FastJsonConfiguration {


    /**
     * fastjson http消息转换器
     *
     * @return {@link HttpMessageConverters}
     */
    @Bean
    //@ConditionalOnProperty(value = "fastjson.enable",havingValue = "true" ,matchIfMissing = true)
    @ConditionalOnMissingBean(value = HttpMessageConverters.class)
    public HttpMessageConverters fastJsonHttpMessageConverter(FastJsonConfig fastJsonConfig){
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastJsonHttpMessageConverter);
    }

    /**
     * fastjson配置
     *
     * @return {@link FastJsonConfig}
     */
    @Bean
    @ConditionalOnMissingBean(value = FastJsonConfig.class)
    public FastJsonConfig fastJsonConfig(List<ValueFilter> valueFilters){
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //修改配置返回内容的过滤
        //WriteNullListAsEmpty  ：List字段如果为null,输出为[],而非null
        //WriteNullStringAsEmpty ： 字符类型字段如果为null,输出为"",而非null
        //DisableCircularReferenceDetect ：消除对同一对象循环引用的问题，默认为false（如果不配置有可能会进入死循环）
        //WriteNullBooleanAsFalse：Boolean字段如果为null,输出为false,而非null
        //WriteMapNullValue：是否输出值为null的字段,默认为false
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,  //禁用循环引用
                SerializerFeature.PrettyFormat,
                SerializerFeature.IgnoreNonFieldGetter,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullListAsEmpty
        );
        if (!valueFilters.isEmpty()){
            ValueFilter[] filters = valueFilters.toArray(new ValueFilter[0]);
            fastJsonConfig.setSerializeFilters(filters);
        }
        return fastJsonConfig;
    }


}
